<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>生成div</title>
    <style>
        .div{
            width: 30px;
            height: 30px;
            background-color: red;
            position: absolute;
        }
    </style>
</head>
<body>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
    <script>
        $(document).click(function(e){
            let left = e.pageX  //获取鼠标点击处X轴位置
            let top = e.pageY   //获取鼠标点击处Y轴位置

            let div = $("<div/>").addClass('div').css({
                left:left+'px',
                top:top+'px'
            })
            
            let timer = setInterval(() => {
                let lr = Math.round(Math.random())  //随机返回0或者1
                left = lr ? left+20 : left-20
                top-=20
                div.css({
                    left:left+'px',
                    top:top+'px'
                })
                if(div.offset().top<0){
                    div.remove()
                    clearInterval(timer)
                }
            }, 200);
            $("body").append(div)
        })
    </script>
</body>
</html>